{% set infotext = {
    'green':  "The last entry is a passing balance check.",
    'red':    "The last entry is a failing balance check.",
    'yellow': "The last entry is not a balance check.",
} %}

{% macro indicator(account) %}
{% set status=ledger.account_uptodate_status(account) %}
{% if status %}
<span class="status-indicator status-{{ status }}" title="{{ infotext[status] }}

Click to copy the balance directives to the clipboard:

{{ balance_directive(account) }}" data-clipboard-text="{{ balance_directive(account) }}"></span>
{% endif %}
{% endmacro %}

{% macro last_account_activity(account_name) %}
{% set last_entry = ledger.last_entry(account_name) %}
{% set last_account_activity = (today() - last_entry.date).days if last_entry else 0 %}
{% if last_account_activity > ledger.fava_options['uptodate-indicator-grey-lookback-days'] %}
    <span class="status-indicator status-gray" title="This account has not been updated in a while. ({{ last_account_activity }} days ago)"></span>
{% endif %}
{% endmacro %}

{% macro balance_directive(account) %}
{% for position in (ledger.all_root_account|get_or_create(account)).balance|units %}
{{ today() }} balance {{ account.ljust(28) }} {% with num=position.units.number|string %}{{ num.rjust(15) }}{% endwith %} {{ position.units.currency }}
{% endfor %}
{% endmacro %}

{% macro account_name(account_name, last_segment=False) %}
<a href="{{ url_for('account', name=account_name) }}" class="account">
  {{ account_name.split(':')[-1] if last_segment else account_name }}
</a>

{% if account_name and ledger.accounts[account_name].meta.get('fava-uptodate-indication') %}
{{ indicator(account_name) }}
{{ last_account_activity(account_name) }}
{% endif %}
{% endmacro %}

{% macro account_name_header(account_name) %}
<span class="droptarget" data-account-name="{{ account_name }}">
  {% set parts = account_name.split(':') %}
  {% for part in parts %}
  {%- if loop.last -%}
  {{ part }}
  {%- else -%}
  {%- set name = ':'.join(parts[:loop.index]) -%}
  <a href="{{ url_for('account', name=name) }}" title="{{ name }}">{{ part }}</a>:
  {%- endif -%}
  {% endfor %}

  {% if account_name and ledger.accounts[account_name].meta.get('fava-uptodate-indication') %}
  {{ indicator(account_name) }}
  {{ last_account_activity(account_name) }}
  {% endif %}

  {% set last_entry = ledger.last_entry(account_name) %}
  {% if last_entry %}
  <span class="last-activity">
    (Last entry: <a href="#context-{{ last_entry|hash_entry }}">{{ last_entry.date }}</a>)
  </span>
  {% endif %}
</span>
{% endmacro %}
